# -*- coding: utf-8 -*-
# $Id: commandparser.py 23 2008-02-13 03:54:22Z gkuznets $

# Copyright 2008, 2009 Grigoriy Kuznetsov (gkuznets@yandex.ru)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from PyQt4 import Qt, QtCore, QtGui
from PyQt4.QtCore import QString, SIGNAL
import re

class CommandParser(QtCore.QObject):
    """Класс для разбора команд с клавиатуры"""
  
    def __init__(self):
        QtCore.QObject.__init__(self)
        # Компилируем регекспы для комманд
        self.send_batch_to_group  = re.compile("^\d\d\d")
        self.help                 = re.compile("^h")
        self.undo                 = re.compile("^u")
        self.highlight            = re.compile("^\d\d")
        self.batch_back           = re.compile("^\d\d ")
        self.add                  = re.compile("^a")
        self.mark                 = re.compile("^f\d\d")
        self.modify               = re.compile("^m\d\d")
        self.second               = re.compile("^second")
        self.mistake              = re.compile("^[^\dhuafs]")
  
    def __call__(self, text):
        try:
            text = str(text)
            print "[" +  text + "]"
            if self.help.search(text):
                self.emit(SIGNAL("help"))
            elif self.undo.search(text):
                self.emit(SIGNAL("undo"))
            elif self.send_batch_to_group.search(text):
                self.emit(SIGNAL("sbtg"), int(text[0:2]), int(text[2])-1)
            elif self.batch_back.search(text):
                self.emit(SIGNAL("bb"), int(text[0:2]))
            elif self.highlight.search(text):
                self.emit(SIGNAL("highlight"), int(text[0:2]))
            elif self.add.search(text):
                self.emit(SIGNAL("add"))
            elif self.mark.search(text):
                self.emit(SIGNAL("mark"), int(text[1:3]))
            elif self.second.search(text):
                self.emit(SIGNAL("second"))
            elif self.mistake.search(text):
                self.emit(SIGNAL("mistake"))
        except:
            self.emit(SIGNAL("mistake"))
